package com.caigou.modules.forum.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.forum.bean.AdItemBean
import com.caigou.modules.forum.bean.NoticeItemBean
import com.caigou.modules.forum.bean.PostBean
import com.caigou.modules.forum.repository.ForumRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/17
 */
@HiltViewModel
class ForumViewModel @Inject constructor(private val repository: ForumRepository) : ViewModel() {

    val forumListLiveData = MutableLiveData<Resource<PostBean>>()
    val historyPostListLiveData = MutableLiveData<Resource<PostBean>>()
    val adListLiveData = MutableLiveData<Resource<MutableList<AdItemBean>>>()
    val noticeListLiveData = MutableLiveData<Resource<MutableList<NoticeItemBean>>>()

    fun getForumList(page: Int, limit: Int, type: Int, sort: Int, currentUserId: String) {
        forumListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getForumList(page, limit, type, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PostBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    forumListLiveData.value = it
                }
        }
    }

    fun getForumHistoryList(page: Int, limit: Int, type: Int, userId: String) {
        historyPostListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getHistoryPostList(page, limit, type, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PostBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    historyPostListLiveData.value = it
                }
        }
    }

    fun getAdList() {
        adListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getAdList()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<AdItemBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    adListLiveData.value = it
                }
        }
    }

    fun getNoticeList() {
        noticeListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getNoticeList()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<NoticeItemBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    noticeListLiveData.value = it
                }
        }
    }
}